
<!DOCTYPE html>
<html>
<head>
    <title>角色修改</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../layui/layui-xtree.js"></script>
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <script src="../../js/jquery-1.8.3.js"></script>
    <script src="../../js/util.js"></script>
    <script>
        layui.use(['form', 'jquery', 'layer','element'], function () {
            var roleId = getUrlParam("roleId");
            var form = layui.form;
            var $ = layui.$;
            var layer = layui.layer;

            function test() {
                var json;
                var url = '/functions';
                $.ajax({
                    type: 'post',
                    url: url,
                    async: false,
                    success: function (data) {
                        json = data;
                    },
                    error: function (data) {
                        layer.msg("获取权限信息出现异常");
                    }
                });
                return json;
            }
            //创建tree
            var xtree = new layuiXtree({
                elem: 'layui-xtree',
                form: form,
                data: test(),
                isopen: false,
                color: "#000",
                icon: {
                    open: "&#xe7a0;",
                    close: "&#xe622;",
                    end: "&#xe621;"
                }
            });
            //初始化页面数据
            $(document).ready(function () {
                var url = '/role/findByRoleId?roleId=' + roleId;
                $.ajax({
                    type: 'post',
                    async: false,
                    url: url,
                    success: function (json) {
                        $("#roleName").val(json.roleName);
                        $("#duty").val(json.duty);
                        var functionIds = json.functionId;
                        var boxes = document.getElementsByClassName('layui-xtree-checkbox');
                        if (functionIds != null) {
                            var functionIdsArr = functionIds.split(",");
                            for (i = 0; i < boxes.length; i++) {
                                for (j = 0; j < functionIdsArr.length; j++) {
                                    if (boxes[i].value == functionIdsArr[j]) {
                                        boxes[i].checked = true;
                                        break;
                                    }
                                }
                            }
                        }
                        form.render();
                    }
                })
            });


            var functionIds = "";
            $('#add').on('click', function () {
                functionIds = '';
                var oCks = xtree.GetChecked();
                for (var i = 0; i < oCks.length; i++) {
                    var deleNode = oCks[i].value;
                    if (functionIds == undefined || functionIds == '') {
                        functionIds = oCks[i].value;
                    } else {
                        if (deleNode == functionIds.substring(0, 2)) {
                            functionIds += oCks[i].value;
                        } else {
                            functionIds += ',' + oCks[i].value;
                        }
                    }
                }
                var url = '/role/modify';
                var roleName = $("#roleName").val();
                var duty = $("#duty").val();
                if (isNull(roleName)) {
                    layer.msg("请输入角色名");
                } else if (duty == "") {
                    layer.msg("请选择级别")
                } else if (isNull(functionIds)) {
                    layer.msg("请选择权限");
                } else {
                    var param = {
                        roleId: roleId,
                        roleName: roleName,
                        duty: duty,
                        functionIds: functionIds
                    };
                    $.ajax({
                        type: 'post',
                        data: param,
                        url: url,
                        success: function (data) {
                            if (data >= 1) {
                                layer.confirm('修改成功', {
                                    btn: ['确认'] //按钮
                                }, function () {
                                    location.href = "roleManage.html";
                                });
                            }
                            else {
                                layer.confirm('修改失败，请重试', {
                                    btn: ['确认'] //按钮
                                });
                            }
                        },
                        error: function (data) {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    });
                }
            });

        });

    </script>
</head>
<body>
<div class="layui-content-body">
    <div class="scroll-box">
        <div class="breadcrumb-box">
              <span class="layui-breadcrumb" lay-separator="|">
      <a href="javascript:returnIndex2()">首页&nbsp;</a>
  <a href=""> &nbsp;系统管理&nbsp; </a>
  <a href="roleManage.html"> &nbsp;角色管理&nbsp; </a>
<a href=""> &nbsp;角色修改&nbsp; </a>
</span>
        </div>
        <div class="content-box">
            <div class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">角色名称</label>
                        <div class="layui-input-block">
                            <input type="text" id="roleName" lay-verify="required"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">级别</label>
                        <div class="layui-input-block">
                            <select id="duty" lay-filter="duty">
                                <option value=""></option>
                                <option value="0">省公司管理员</option>
                                <option value="1">地市管理员</option>
                                <option value="2">区县管理员</option>
                                <option value="3">项目管理员</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-form" action="">
                    <div id="layui-xtree"></div>
                </div>
                <div class="layui-block" pane="">
                    <button class="layui-btn layui-btn-normal" lay-filter="add" id="add">保存&nbsp;</button>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
